<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="drawerShow = true"
          ></el-button>
        </div>
      </template>
      <template>
        <el-table :data="list" border style="width: 100%">
          <el-table-column prop="stem" label="标题" width="180">
          </el-table-column>
          <el-table-column prop="creator" label="作者" width="180">
          </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
          <el-table-column>
            <template slot-scope="scope">
              <div class="actions">
                <i class="el-icon-view"></i>
                <i class="el-icon-edit-outline" @click="getDetail(scope)"></i>
                <i class="el-icon-delete" @click="deleteListData(scope)"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <el-row type="flex" justify="center">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="changePage"
        :current-page="requestData.current"
      ></el-pagination>
    </el-row>

    <el-drawer
      :title="title === '编辑面经' ? '编辑面经' : '新增面经'"
      :visible.sync="drawerShow"
      :direction="direction"
      :before-close="handleClose"
      :wrapperClosable="true"
    >
      <!-- <span>我来啦!</span> -->
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            ref="myQuillEditor"
            v-model="form.content"
            @blur="$refs.form.validateField()"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="onClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  interviewData,
  createInterview,
  deleteInterview,
  getInterviewDetail
} from '@/api/interview'
export default {
  name: 'articleIndex',
  components: {
    quillEditor
  },
  data () {
    return {
      // 列表
      list: [],
      // 总页数
      total: 0,
      // 请求数据
      requestData: {
        // 页码
        current: 1,
        // 每页显示条数
        pageSize: 10,
        // 搜索关键字
        keyWord: '',
        // 题干
        stem: '',
        sorter: ''
      },
      // pagination数据
      form: {
        stem: '',
        content: ''
      },
      // 表单效验
      rules: {
        stem: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      // 抽屉方向
      direction: 'rtl',
      // 抽屉显示
      drawerShow: false,
      // 抽屉标题
      title: ''
    }
  },
  created () {
    this.tableData()
  },
  methods: {
    async tableData () {
      const { data } = await interviewData(this.requestData)
      this.list = data.data.rows
      this.total = data.data.total
    },
    async onSubmit () {
      try {
        await this.$refs.form.validate()
        this.requestData.current = 1
        await createInterview(this.form)
        this.$message.success('创建成功')
        this.drawerShow = false
      } catch (error) {}
      this.$refs.form.resetFields()
      this.tableData()
    },
    async onClose () {
      await this.$refs.form.resetFields()
      this.drawerShow = false
    },
    // ()
    async deleteListData (scope) {
      await deleteInterview({ id: scope.row.id })
      this.$message.success('删除成功(测试用,实际不会删除)')
      this.list.splice(scope.$index, 1)
      this.tableData()
    },
    async getDetail (scope) {
      const { data } = await getInterviewDetail({ id: scope.row.id })
      this.form.stem = data.data.stem
      this.form.content = data.data.content
      this.title = '编辑面经'
      this.id = data.data.id
      console.log(this.form)
      console.log(data)
      this.drawerShow = true
    },
    handleClose (done) {
      this.$refs.form.resetFields()
      done()
    },
    changePage (val) {
      this.requestData.current = val
      this.tableData()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
